<?php

/**
 * Status.
 * 0 - invitation has been sent
 * 1 - people click the invitation
 * 
 * @author james
 *
 */
class AuthorizationMap extends Zend_Db_Table_Abstract {
	
	const MAIL_UNTOUCHED = 0;
	const MAIL_ACCESSED = 1;
	
	protected $_name = 'authorization_map';
	protected $_primary = 'id';
	
	protected $_referenceMap = array(
		'Invitation' => array(
			'columns' => 'invitation_id',
			'refTableClass' => 'Invitation',
			'refColumns' => 'id'
		),
		'User' => array(
			'columns' => 'user_id',
			'refTableClass' => 'User',
			'refColumns' => 'id'
		)
	);
	
	/**
	 * Find by id.
	 * 
	 * @param $id id
	 * @return return object
	 */
	public static function findById($id) {
		$authorizationMap = new AuthorizationMap();
		return $authorizationMap->find($id)->current();
	}
	
	/**
	 * 
	 * @param $invitation_id
	 * @param $type
	 * @param $key
	 * @return unique object
	 */
	function findByUnique($invitation_id, $user_id) {
		$select = $this->select();
		$select->where('invitation_id = ?', $invitation_id)
		->where('user_id = ?', $user_id);
		return $this->fetchRow($select);
	}
	

	/**
	 * Find by value, should be unique.
	 * 
	 * @param $value
	 */
	function findByValue($value) {
		$select = $this->select();
		$select->where('value = ?', $value);
		return $this->fetchRow($select);
	}
	
	/**
	 * Delete authorization map from invitation id and user id.
	 * 
	 * @param $invitation_id invitation id
	 * @param $user_id user id
	 */
	function deleteByInvitationAndUserId($invitation_id, $user_id) {
		$row = $this->findByUnique($invitation_id, $user_id);
		$row->delete();
	}
	
	/**
	 * Find if the given invitaion and invited user has the same key in db as provided.
	 * 
	 * @param $invitation_id $invitation id
	 * @param $invited_user_id $user id
	 * @param $key key value from request
	 */
	function keyMatch($invitation_id, $invited_user_id, $key) {
		$keyMatch = false;
		if (isset($key)) {
			$select = $this->select();
			$select->where('invitation_id=?', $invitation_id)
			->where('user_id=?', $invited_user_id);
			$row = $this->fetchRow($select);
			if ($row->value == $key) {
				$keyMatch = true;
			}
		}
		return $keyMatch;
	}
	
	/**
	 * Update status by given unique value.
	 * 
	 * @param $key
	 * @param $status
	 */
	function setStatus($key, $status) {
		$data = $this->findByValue($key);
		$data->status = $status;
		return $data->save();
	}
	
	/**
	 * Add user authorization, note that the value must be unique.
	 * 
	 * @param $user_id
	 * @param $invitation_id
	 * @param $value
	 * @return return object
	 */
	function addAuthorization($user_id, $invitation_id, $email) {
		$checkUnique = $this->findByUnique($invitation_id, $user_id);
		if (!isset($checkUnique)) {
			$value = $this->generateUniqueValue($invitation_id, $email, time());
			$checkValue = $this->findByValue($value);
			while (isset($checkValue)) {
				$value = $this->generateUniqueValue($invitation_id, $email, time());
				$checkValue = $this->findByValue($value);
			}
			$data = array(
				'user_id'=>$user_id,
				'invitation_id'=>$invitation_id,
				'value'=>$value
			);
			$id = $this->insert($data);
			return AuthorizationMap::findById($id);
		} else {
			return null;
		}
	}
	
	/**
	 * 
	 * @param $invitation_id
	 * @param $email
	 * @param $random
	 */
	private function generateUniqueValue($invitation_id, $email, $random) {
		$seed = $invitation_id.$email.$random;
		return md5($seed);
	}
}
?>